
var Take6Model = require("Take6Model");

cc.Class({
    extends: cc.Component,

    properties: 
    {
        resultCell : {
            default: null,
            type: cc.Prefab
        },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad : function () 
    {
        var sortPlayerInfo = function (info1, info2)
        {
            if (info1.nPlayerScore > info2.nPlayerScore) return -1
            else if (info1.nPlayerScore < info2.nPlayerScore) return 1
            else return 0
        };

        var rgPlayersInfo = [];
        for (var i = 0; i < Take6Model.m_rgPlayersInfo.length; i++)
        {
            rgPlayersInfo.push(Take6Model.m_rgPlayersInfo[i]);
        }

        rgPlayersInfo.sort(sortPlayerInfo);

        var lastScore = 1000;
        var nCurrentRank = 0;
        for (var i = 0; i < Take6Model.playerCount; i++)
        {
            var cell = cc.instantiate(this.resultCell);
            this.node.addChild(cell);

            var offsetY = 10 + cell.height;

            cell.setPosition(cc.p(0, (2 - i) * offsetY));

            var playerInfo = rgPlayersInfo[i];
            cc.log("PlayerInfo :", playerInfo);

            // 处理并列情况
            if (playerInfo.nPlayerScore != lastScore)
            {
                nCurrentRank = i + 1;
                lastScore = playerInfo.nPlayerScore;
            }

            var cellInfo = cell.getComponent("ResultCell");
            cellInfo.setScore(playerInfo.nPlayerScore);
            cellInfo.setName(playerInfo.strPlayerName);
            cellInfo.setIcon(playerInfo.nPlayerIcon);
            cellInfo.setRank(nCurrentRank);

            if (playerInfo.nPlayerIndex == Take6Model.myPlayerIndex)
            {
                cellInfo.setMyCell();
            }
        }
    },

    onOKButtonPressed : function ()
    {
        Take6Model.playerExitRoom();
        Take6Model.m_bGameLayerOpened = false;
    },

    start () {

    },

    // update (dt) {},
});
